#拼多多增量信息


import time,requests,json
from common.config import *
from common.Helper import Helper
import pprint

class OrderIncrement( object ):

    def __init__(self):

        pass

    # 获取签名信息
    def signInfo(self,task):

        ts = int(time.time())
        #设置参数
        params = {

            "type": "pdd.order.number.list.increment.get",
            "data_type": "JSON",
            "timestamp": "{0}".format(ts),
            "client_id": "{0}".format(secret_key['client_id']),
            "access_token": "{0}".format(task['access_token']),
            "is_lucky_flag": "0",
            "order_status": "5",
            "start_updated_at": "{0}".format(task['start_time']),
            "end_updated_at": "{0}".format(task['end_time']),
            "page": "{0}".format(task['page']),
            "refund_status": "5",
        }

        sign = Helper.genSign( params )

        params['sign'] = sign

        return params

    # 增量详情
    def requestJson(self,task):

        resp = {'type': 101, 'msg': '增量任务','content':{}}

        param = self.signInfo( task )

        r = requests.post(url=api_router,data=json.dumps(param),headers=header )

        print(r.json())

        # 应用错误
        if r.status_code == 200 and 'error_response' in json.dumps( r.json(),ensure_ascii=False):

            msg = dict( error=r.json()['error_response']['error_code'],error_msg=r.json()['error_response']['error_msg'])

            # print('error:',r.json()['error_response']['error_code'])
            resp['content']['content'] = task
            resp['content']['errcode'] = msg['error_code']
            resp['content']['errmsg'] = msg['error_msg']
            resp['content']['data'] = {}
            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            print( msg )

            return resp

        elif r.status_code == 200 and  'error_response' not in r.json():

            list = r.json()['order_sn_increment_get_response']['order_sn_list']

            if len( list ) == 0:

                print('此时间段无订单增量信息')

                # 合并结果
                resp['content']['content'] = task
                resp['content']['errcode'] = 0
                resp['content']['errmsg'] = '任务完成'
                resp['content']['data'] = {}

                content = Helper.changeBase64(json.dumps(resp['content']))

                resp['content'] = content.decode('ascii')

                return resp

            # 定义任务列表
            pprint.pprint( list )

            resp['content']['content'] = task
            resp['content']['data'] = (list)
            resp['content']['errcode'] = 0
            resp['content']['errmsg'] = '任务完成'

            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp

        else:

            return ('拼多多数据返回结果错误~~~')

